<template>
    <div class="p-2">
        <transition
            :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave"
        >
            <div class="search">
                <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="70px">
                    <el-form-item label="选择客户" prop="customerId">
                        <el-select v-model="queryParams.customerId" placeholder="请选择">
                            <el-option v-for="item in customerList" :key="item.id" :label="item.customerName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否异常" prop="normal">
                        <el-select v-model="queryParams.normal" placeholder="请选择">
                            <el-option label="无异常" value="0"></el-option>
                            <el-option label="同金额收款多次" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="发货详情" prop="approvalStatus">
                        <el-select v-model="queryParams.approvalStatus" placeholder="请选择">

                        </el-select>
                    </el-form-item>
                    <el-form-item label="对账状态" prop="reconciliationStatus">
                        <el-select v-model="queryParams.reconciliationStatus" placeholder="请选择">
                            <el-option value="0" label="未对账"></el-option>
                            <el-option value="1" label="已对账"></el-option>
                            <el-option value="2" label="已回款"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="制单人" prop="createdByName">
                        <el-select v-model="queryParams.createdByName" placeholder="请选择">
                            <el-option v-for="user in roleKeyUserList" :key="user.userId" :label="user.userName" :value="user.userName"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <el-table :data="orderList">
                <el-table-column label="制单日期" prop="createdByTime" align="center"></el-table-column>
                <el-table-column label="送货单号" prop="orderNumber" align="center"></el-table-column>
                <el-table-column label="制单人" prop="createdByName" align="center"></el-table-column>
                <el-table-column label="客户名称" align="center">
                    <template #default="scope">
                        {{ customerList.filter((item)=>item.id == scope.row.customerId)[0]?.customerName }}
                    </template>
                </el-table-column>
                <el-table-column label="当前所属公司" prop="company" align="center"></el-table-column>
                <el-table-column label="发货方式" align="center">
                    <template #default="scope">
                        {{ pay_type.filter((item: any)=>item.value == scope.row.sendMehod)[0]?.label }}
                    </template>
                </el-table-column>
                <el-table-column label="发货详情" align="center">
                    
                </el-table-column>
                <el-table-column label="发货状态" align="center">
                    <template #default="scope">
                        <el-button link v-if="scope.row.deliverStatus == '0'" type="danger">未发货</el-button>
                        <el-button link v-else-if="scope.row.deliverStatus == '1'" type="success">可发货</el-button>
                        <el-button link v-else-if="scope.row.deliverStatus == '2'" type="primary">已发货</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="对账状态" align="center">
                    <template #default="scope">
                        <el-button link v-if="scope.row.reconciliationStatus == '0'" type="danger">未对账</el-button>
                        <span link v-else-if="scope.row.reconciliationStatus == '1'" class="purple" >已对账</span>
                        <span link v-else-if="scope.row.reconciliationStatus == '2'" class="warning" >已回款</span>
                    </template>
                </el-table-column>
                <el-table-column label="异常状态" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.normal == '0'" class="normal">无异常</span>
                        <span v-else-if="scope.row.normal == '1'" class="alert">同金额收款多次</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="180px">
                    <template #default="scope">
                        <el-tooltip content="打印" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Printer" type="primary" @click="handleClickPrint(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="物流回单" placement="top">
                            <el-button link :style="{fontSize:'15px'}" type="success" icon="Document" @click="handleClickLogisticsInfo(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Delete" type="danger" @click="handleClickDelete(scope.row)" ></el-button>
                        </el-tooltip>
                        <el-tooltip content="查看付款凭证" placement="top">
                            <el-button link :style="{fontSize:'15px'}" icon="Money" type="primary" @click="handleClickCheckProof(scope.row)" ></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <printOrder :visible="printOrderVisible" @close="handleClosePrint"/>
        <checkProof :visible="checkProofVisible" @close="handleCloseCheckProof" />
        <checkLogisticsInfo :visible="clickLogisticsInfoVisible" :sendType="send_type" @close="handleCloseLogisticsInfo" />
    </div>
</template>

<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import useCustomerStore from '@/store/modules/customers';
import useCRMStore from '@/store/modules/crm';
import printOrder from './components/printOrder.vue';
import checkProof from './components/checkProof.vue';
import checkLogisticsInfo from './checkLogisticsInfo.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { pay_type,send_type } = toRefs<any>(proxy?.useDict('pay_type', 'send_type','offline_payment_type','online_payment_type'));
const queryFormRef = ref<FormInstance>();
const { getCustomerData } = useCustomerStore()
const { customerList } = toRefs(useCustomerStore());
const { getUserListByRoleKey } = useCRMStore();
const { roleKeyUserList } = toRefs(useCRMStore());

const data = reactive({
    queryParams: {
        customerId: undefined,
        normal: undefined,
        approvalStatus: undefined,
        deliverStatus: undefined,
        reconciliationStatus: undefined,
        createdByName: undefined,
        orderNumber: undefined,
        pageNum: 1,
        pageSize: 30
    }
});

const orderList = ref<any[]>([]);

const { queryParams } = toRefs(data); 

const printOrderVisible = ref(false);
const checkProofVisible = ref(false);
const clickLogisticsInfoVisible = ref(false);

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};


/** 点击打印 */
const handleClickPrint = async (row: any) => {
    printOrderVisible.value = true;
};

const handleClosePrint = () => {
    printOrderVisible.value = false;
};

/** 点击查看物流回单 */
const handleClickLogisticsInfo = async (row: any) => {
    clickLogisticsInfoVisible.value = true;
};

const handleCloseLogisticsInfo = () => {
    clickLogisticsInfoVisible.value = false;
};

const handleClickDelete = async (row: any) => {
    await proxy?.$modal.confirm('确认将此订单放入回收站么')
};


/** 点击查看付款凭证 */
const handleClickCheckProof = async (row: any) => {
    checkProofVisible.value = true;
};

const handleCloseCheckProof = () => {
    checkProofVisible.value = false;
};

onMounted(()=>{
    getCustomerData();
    getUserListByRoleKey('business_role');
    const dataList = [];
    for(let i=1;i<=30;i++){
        const data = {
            id: i,
            createdByTime: '2024-11-07',
            orderNumber: `00000${i}`,
            createdByName: 'hnl',
            customerId: '1692055279319113730',
            company: 'A公司',
            price: 5000,
            sendMehod: '1',
            approvalStatus: '1',
            deliverStatus: '0',
            reconciliationStatus: '1',
            normal: '0'
        };
        dataList.push(data);
    };
    orderList.value = dataList;
});
</script>

<style scoped>

</style>